﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ET.Entity;
using ET.DbUtility;
using System.Data.SqlClient;

namespace ET.DataAccess
{
    public class DaServiceTracking
    {
        public int Insert(EtServiceTracking serviceTracking)        
        {
            string sql = string.Format("INSERT INTO EtServiceTracking(EmployeeId, ServiceId, ClientName, Note, Ip, RegisterTime) " +
                "VALUES(@EmployeeId, @ServiceId, @ClientName, @Note, @Ip, @RegisterTime)");

            SqlParameter[] sqlParameterSet = 
            { 
                new SqlParameter("@EmployeeId", serviceTracking.EmployeeId),
                new SqlParameter("@ServiceId", serviceTracking.ServiceId),
                new SqlParameter("@ClientName", serviceTracking.ClientName),
                new SqlParameter("@Note", serviceTracking.Note),
                new SqlParameter("@Ip", serviceTracking.Ip),
                new SqlParameter("@RegisterTime", serviceTracking.RegisterTime)
            };

            return EtSqlHelper.ExecuteNonQuery(sql,sqlParameterSet);
        }

        public List<EtServiceTracking> GetAllUnDeleted()
        {
            string sql = "SELECT * FROM EtServiceTracking WHERE IsDeleted = 0";
            return EtSqlEntityHelper<EtServiceTracking>.GetEntitySet(sql);
        }

        public List<EtServiceTracking> GetServiceTrackingByUserId(string userId)
        {
           string sql = string.Format(@"SELECT TOP 50 e.EmployeeName, t.ClientName, s.ServiceName, t.Ip, t.RegisterTime, t.Note
                        FROM EtServiceTracking t
                        LEFT JOIN EtEmployeeInfo e ON t.EmployeeId = e.Id
                        LEFT JOIN EtServiceInfo s ON t.ServiceId = s.Id
                        WHERE e.Id=CAST('{0}' AS uniqueidentifier) AND t.IsDeleted =0 AND e.IsDeleted = 0 AND s.IsDeleted = 0
                        ORDER BY t.RegisterTime DESC", userId);
           return EtSqlEntityHelper<EtServiceTracking>.GetEntitySet(sql);
        }
    }
}
